<template>
    <div class="common-layout">
      <div class="login">

        <el-form
            ref="ruleFormRef"
            style="max-width: 600px"
            :model="ruleForm"
            :rules="rules"
            label-width="auto"
            class="demo-ruleForm"
            :size="formSize"
            status-icon
        >
            <el-form-item label="用户名" prop="name">
                <el-input v-model="ruleForm.name" />

            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="ruleForm.password" type="password"/>
            </el-form-item>
            <el-form-item style="margin-left: 150px;">
            <el-button type="primary" @click.enter="submitForm(ruleFormRef)">
                登录
            </el-button>
            <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
              </el-form>
          </div>
    </div>
</template>
<script>
import { reactive } from 'vue';
import { ElForm } from 'element-plus';
import axios from 'axios';
import router from '@/router'
export default {
  components: {
    ElForm,
    // 其他需要的 Element Plus 组件
  },
  data() {
    return {
      formSize: 'default',
      ruleForm: reactive({
        name: 'Hello',
      }),
      locationOptions: ['Home', 'Company', 'School'],
      options: Array.from({ length: 10000 }).map((_, idx) => ({
        value: `${idx + 1}`,
        label: `${idx + 1}`,
      })),
      rules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度要在3-10', trigger: 'blur' },
        ],       
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 10, message: '长度要在3-10', trigger: 'blur' },
        ],       
      },
      ruleFormRef: null,
      userlist:[]
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.ruleFormRef = this.$refs.ruleFormRef;
    });
    axios.get('users.json').then(res=>{
      this.userlist=res.data.userslist
    })


// 向给定ID的用户发起请求

  },
  methods: {
    async submitForm() {
      if (!this.ruleFormRef) return;
      await this.ruleFormRef.validate((valid, fields) => {
        if (valid) {
          //此处完成用户名和密码的匹配,如果匹配,则跳转到首页
          //使用some函数来对数组进行变化,查找符合条件的数据,如果找到了,则返回true,否则返回false
          //
          let flag=this.userlist.some(v =>v.name=this.userslist.name && v.password==this.userslist.password)
          // 对flag判断
          if(!flag){
            alert('请输入正确的用户名和密码')
          }else{
            //跳转
            sessionStorage.setItem('username',this.ruleForm.name)
            router.push('/')

          }
          console.log('submit!');
        } else {
          console.log('error submit!', fields);
        }
      });
    },
    resetForm() {
      if (!this.ruleFormRef) return;
      this.ruleFormRef.resetFields();
    },
  },
};
</script>
<style scoped>
.common-layout{
    background: url('../../assets/OIP-C.png') no-repeat center;
    height: 100%;
    width: 100%;
    background-size: cover;
    display: flex;
    position: absolute;
    left: 0px;
    top: 0px;
}
.login{
  width: 400px;
  height: 200px;
  margin: auto;
  text-align: center;
}



</style>